//#include "modbus-rtu-private.h"
//#include "modbus-private.h"
//#include "modbus.h"

/**
 * @brief Modbus协议为大端模式,PC系统和ARM为小端模式,发送和接收需要转换顺序
 *
 *
 */
#if defined(BUILD_DLL) && !defined(ISSWIG)
#define DLL_EXPORT  __declspec(dllexport)
#else
#define DLL_EXPORT
#endif


#ifdef __cplusplus
extern "C"
{
#endif

#define MODBUS_FLAG_CONNECT         0x1 //已连接
#define MODBUS_FLAG_IP_MODE         0x2    //网络通信使用IP方式或域名方式
#define MODBUS_FLAG_PUT_FIRST       0x4    //先写后读模式
#define MODBUS_FLAG_LITTLE_ENDIAN 0x8 //小端格式, 0x 12 34 56 78 ->0x 78 56 34 12 ,大端模式按照顺序不变
#define MODBUS_FLAG_BYTE_SWAP   0x10 //高低字节交换
#define MODBUS_FLAG_RX_DISABLE    0x20 //禁止接收数据(禁止从外部设备读取数据)
#define MODBUS_FLAG_TX_DISABLE    0x40 //禁止向外部设备发送数据
#define MODBUS_FLAG_TX_SENDOK    0x80 //发送数据成功
#define MODBUS_FLAG_WORK		0x100	//工作状态设置

typedef struct modbus_server *modbus_server_t;

struct modbus_server
{
    void *ctx;
    int net;
    unsigned int req_length;
    unsigned char id;       //站址
    uint16_t status;  //工作状态
    uint16_t delay;       //发送前延时，单位ms
    uint16_t wait;    //等待时间，超时视为无连接

    uint32_t flag;

    uint16_t(*READ_INPUT_REGISTER)(modbus_server_t ctx,uint16_t addr,uint8_t len,uint16_t * dst);
    int(*READ_HOLDING_REGISTER)(modbus_server_t ctx,uint16_t addr,uint8_t len,uint16_t * dst);
//	    int (*WRITE_SINGLE_REGISTER)(modbus_server_t ctx,const uint16_t *dats,uint16_t addr,uint8_t len);
    int (*WRITE_MULTIPLE_REGISTERS)(modbus_server_t ctx,const uint16_t *dats,uint16_t addr,uint8_t len);
    unsigned char(*read_single_coil)(modbus_server_t ctx,unsigned int);
    void (*write_single_coil)(modbus_server_t ctx,uint16_t,unsigned char);
    unsigned char(*input_bits_rd)(modbus_server_t ctx,uint16_t);


    void (*modbus_get_word)(const uint16_t *src,uint8_t *);
    void (*modbus_set_word)(const uint8_t * f, uint16_t *dest);
    void (*modbus_get_dword)(const uint16_t *src,uint8_t *);
    void (*modbus_set_dword)(const uint8_t * f, uint16_t *dest);
    void (*modbus_get_lword)(const uint16_t *src,uint8_t *);
    void (*modbus_set_lword)(const uint8_t * f, uint16_t *dest);
} ;

uint16_t get_int_data(modbus_server_t ctx,uint16_t addr,uint8_t len,uint16_t * dst);
int set_int_data(modbus_server_t ctx, const uint16_t *val,uint16_t i,uint8_t len);
int tab_registers_wr(modbus_server_t  ctx,const uint16_t *vals,uint16_t addr,uint8_t len);


#define PARENT_STRUCT_OF(obj, type, member)   \
  ((type *)((char *)(obj) - (unsigned long)(&((type *)0)->member)))

uint16_t input_registers_rd(modbus_server_t ctx,uint16_t addr,uint8_t len,uint16_t * dst);
///!modbus回调函数,接收校准系数
int8_t modbus_ai_calication_write(modbus_server_t ctx, const uint16_t *val,uint16_t i,uint8_t len);
int8_t modbus_ai_calication_read(modbus_server_t ctx, uint16_t *val,uint16_t i,uint8_t len);
///!允许修改校准系数
int8_t modbus_ai_calication_enable(modbus_server_t ctx, const uint16_t *val,uint16_t i,uint8_t len);
modbus_server_t  modbus_server_init(modbus_server_t server,const char *name, const char *dev,int lendian,int swap,unsigned int baudrate,unsigned short dataBits,unsigned char stopBits,char parity);


///!@ modbus access interface
int modbus_read_history(modbus_server_t ctx,uint16_t  addr,uint8_t len,uint16_t * dst);
int modbus_write_history(modbus_server_t ctx,uint16_t addr, const uint16_t *val,uint8_t len);


#ifdef __cplusplus
};
#endif

